<?php
class EmployeedetailsController extends AppController {

	var $name = 'Employeedetails';
        //var $uses = array('User', 'Employeedetail');

	function index() {
		$this->Employeedetail->recursive = 0;
		$this->set('employeedetails', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid employeedetail', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('employeedetail', $this->Employeedetail->read(null, $id));
	}

	function add($employee_id = null) {
		if (!empty($this->data)) {
			$this->Employeedetail->create();
			if ($this->Employeedetail->save($this->data)) {
				$this->Session->setFlash(__('The employeedetail has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employeedetail could not be saved. Please, try again.', true));
			}
		}
		$employees = $this->Employeedetail->Employee->find('list');
		$users = $this->Employeedetail->User->find('all');
                $superiors[] = null;
                $superior_counter = 0;
                foreach ($users as $user)
                {
                    $is_superior = false;
                    foreach ($user['Group'] as $group)
                    {
                        if ($group['id'] == 2)
                            $is_superior = true;
                    }
                    if($is_superior)
                    {
                        $temp = $user['User']['last_name'] . ", " . $user['User']['first_name'];
                        $superiors[$superior_counter] = $temp;
                        $superior_counter++;
                    }

                }
                $users = $superiors;
               	$departments = $this->Employeedetail->Department->find('list');
		$saftyinstructors = $this->Employeedetail->Saftyinstructor->find('list');
		$qualitymanagers = $this->Employeedetail->Qualitymanager->find('list');
		$degrees = $this->Employeedetail->Degree->find('list');
		$contracts = $this->Employeedetail->Contract->find('list');
		$hardwares = $this->Employeedetail->Hardware->find('list');
		$mailinglists = $this->Employeedetail->Mailinglist->find('list');
		$sapservers = $this->Employeedetail->Sapserver->find('list');
		$this->set(compact('employees', 'users', 'departments', 'saftyinstructors', 'qualitymanagers', 'degrees', 'contracts', 'hardwares', 'mailinglists', 'sapservers'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employeedetail', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Employeedetail->save($this->data)) {
				$this->Session->setFlash(__('The employeedetail has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employeedetail could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employeedetail->read(null, $id);
		}
		$employees = $this->Employeedetail->Employee->find('list');
		$users = $this->Employeedetail->User->find('all');
                $superiors[] = null;
                $superior_counter = 0;
                foreach ($users as $user)
                {
                    $is_superior = false;
                    foreach ($user['Group'] as $group)
                    {
                        if ($group['id'] == 2)
                            $is_superior = true;
                    }
                    if($is_superior)
                    {
                        $temp = $user['User']['last_name'] . ", " . $user['User']['first_name'];
                        $superiors[$superior_counter] = $temp;
                        $superior_counter++;
                    }

                }
                $users = $superiors;
		$departments = $this->Employeedetail->Department->find('list');
		$saftyinstructors = $this->Employeedetail->Saftyinstructor->find('list');
		$qualitymanagers = $this->Employeedetail->Qualitymanager->find('list');
		$degrees = $this->Employeedetail->Degree->find('list');
		$contracts = $this->Employeedetail->Contract->find('list');
		$hardwares = $this->Employeedetail->Hardware->find('list');
		$mailinglists = $this->Employeedetail->Mailinglist->find('list');
		$sapservers = $this->Employeedetail->Sapserver->find('list');
		$this->set(compact('employees', 'users', 'departments', 'saftyinstructors', 'qualitymanagers', 'degrees', 'contracts', 'hardwares', 'mailinglists', 'sapservers'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for employeedetail', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Employeedetail->delete($id)) {
			$this->Session->setFlash(__('Employeedetail deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Employeedetail was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

        function supindex() {
            $userid = $this->Auth->user('id');
            //$employeedetails = $this->Employeedetail->find('all', array('conditions' => array('Employeedetail.superior' => $userid)));
            $this->set('employeedetails', $this->paginate('Employeedetail', array('Employeedetail.user_id' => $userid, 'Employee.superior_complete' => '0')));
            //var_dump($this->Auth->user('id'));
	}

        function supedit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employeedetail', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Employeedetail->save($this->data)) {
				$this->Session->setFlash(__('The employeedetail has been saved', true));
				$this->redirect(array('action' => 'supindex'));
			} else {
				$this->Session->setFlash(__('The employeedetail could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employeedetail->read(null, $id);
		}
		$employees = $this->Employeedetail->Employee->find('list');
		$departments = $this->Employeedetail->Department->find('list');
		$saftyinstructors = $this->Employeedetail->Saftyinstructor->find('list');
		$qualitymanagers = $this->Employeedetail->Qualitymanager->find('list');
		$mailinglists = $this->Employeedetail->Mailinglist->find('list');
		$sapservers = $this->Employeedetail->Sapserver->find('list');
                $degrees = $this->Employeedetail->Degree->find('list');
		$contracts = $this->Employeedetail->Contract->find('list');
		$hardwares = $this->Employeedetail->Hardware->find('list');
		$this->set(compact('employees', 'departments', 'saftyinstructors', 'qualitymanagers', 'degrees', 'contracts', 'hardwares', 'superiors', 'mailinglists', 'sapservers'));
	}
        function supcomplete($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employeedetail', true));
			$this->redirect(array('action' => 'index'));
		}
		if (empty($this->data)) {
                        $this->data = $this->Employeedetail->read(null, $id);
                        $this->data['Employee']['superior_complete'] = 1;
                        if ($this->Employeedetail->Employee->save($this->data)) {
				$this->Session->setFlash(__('The Employee has been marked completed', true));
				$this->redirect(array('action' => 'supindex'));
			} else {
				$this->Session->setFlash(__('The employeedetail could not be saved. Please, try again.', true));
			}
		}
                //@TODO Email an Ines
		
	}

        function send_superior($id) {
            /**
             * Sende Eine EMail für den User $ID an den Vorgesetzen find(id) -->
             */
            $employeedetail = $this->Employeedetail->find('first', array('conditions' => array('Employeedetail.id' => $id)));
            //echo "<br>Sende Email an: ".$employeedetail['Superior']['email']."<br>";
            //debug($employeedetail);
        }
}
?>